package gui;
import java.awt.*;

import javax.swing.*;
import javax.swing.table.*;

public class MatrixWindow extends JFrame {

  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

public MatrixWindow(final double[][] dataMatrix, final String[] headersMatrix, String sg_num) {
    super("Distance matrix " +sg_num);
    setSize(500, 400);

    TableModel tm = new AbstractTableModel() {
      /**
		 * 
		 */
    	private static final long serialVersionUID = 1L;
      public int getColumnCount() { return headersMatrix.length; }
      public int getRowCount() { return headersMatrix.length-1; }
      public String getColumnName(int col) { return headersMatrix[col]; 

     }
     
      
      // Synthesize some entries using the data values & the row #
      public Object getValueAt(int row, int col) { 
        
    	  if(col==0 && (row+1 < headersMatrix.length) )
    	  {
    		  return headersMatrix[row+1];
    	  }
    	  else 
    	  {
    		  if(col!=0 && (col-1 < headersMatrix.length) && (row+1 < headersMatrix.length))
    			  return dataMatrix[row+1][col-1]; 
    	  }
    	  
    	 return "";
      }
    };

    // Create a column model for the main table. This model ignores the first
    // column added, and sets a minimum width of 20 pixels for all others.
    TableColumnModel cm = new DefaultTableColumnModel() {
      /**
		 * 
		 */
		private static final long serialVersionUID = 1L;
	boolean first = true;
      public void addColumn(TableColumn tc) {
        // Drop the first column . . . thatâ€™ll be the row header
        if (first) { first = false; return; }
        tc.setMinWidth(20);  // just for looks, really...
        super.addColumn(tc);
      }
    };

    // Create a column model that will serve as our row header table. This
    // model picks a maximum width and only stores the first column.
    TableColumnModel rowHeaderModel = new DefaultTableColumnModel() {
      /**
		 * 
		 */
		private static final long serialVersionUID = 1L;
	boolean first = true;
      public void addColumn(TableColumn tc) {
        if (first) {
          tc.setMaxWidth(tc.getPreferredWidth());
          super.addColumn(tc);
          first = false;
        }
        // Drop the rest of the columns . . . this is the header column only
      }
    };

    JTable jt = new JTable(tm, cm);

    // Set up the header column and get it hooked up to everything
    JTable headerColumn = new JTable(tm, rowHeaderModel);
    jt.createDefaultColumnsFromModel();
    headerColumn.createDefaultColumnsFromModel();

    // Make sure that selections between the main table and the header stay
    // in sync (by sharing the same model)
    jt.setSelectionModel(headerColumn.getSelectionModel());

    // Make the header column look pretty
	//headerColumn.setBorder(BorderFactory.createEtchedBorder());
    headerColumn.setBackground(Color.lightGray);
    headerColumn.setColumnSelectionAllowed(false);
    headerColumn.setCellSelectionEnabled(false);

    // Put it in a viewport that we can control a bit
    JViewport jv = new JViewport();
    jv.setView(headerColumn);
    jv.setPreferredSize(headerColumn.getMaximumSize());

    // With out shutting off autoResizeMode, our tables wonâ€™t scroll
    // correctly (horizontally, anyway)
    jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    // We have to manually attach the row headers, but after that, the scroll
    // pane keeps them in sync
    JScrollPane jsp = new JScrollPane(jt);
    jsp.setRowHeader(jv);
	jsp.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER,
				  headerColumn.getTableHeader());
    getContentPane().add(jsp, BorderLayout.CENTER);
  }

//  public static void main(String args[]) {
//	  double[][] d = new double[3][3];
//	  	d[0][0] = 0;
//	  	d[0][1] = 0;
//		
//		d[1][0] = 0.1;
//		d[1][1] = 0.2;
//		
//		d[2][0] = 0.4;
//		d[2][1] = 0.5;
//		
//		String[] h = new String[3];
//		h[0] = "test #";
//		h[1] = "test1";
//		h[2] = "test2";
//		
//		MatrixWindow mw = new MatrixWindow(d,h); 
//		mw.setVisible(true);
//  }

}